package com.aaa.ssm.service.impl;

import com.aaa.ssm.entity.Order;
import com.aaa.ssm.entity.Registration;
import com.aaa.ssm.service.OrderService;
import com.aaa.ssm.util.ConstantUtil;
import com.aaa.ssm.util.HttpClient;
import com.github.wxpay.sdk.WXPayUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @ fileName:OrderServiceIml
 * @ description:
 * @ author:zhz
 * @ createTime:2023/10/16 9:19
 * @ version:1.0.0
 */
@Service
@Log4j2
public class OrderServiceImpl implements OrderService {
    @Override
    public Map getNativeByOrder(Order order) {
        try {
            //统一下单   生成预支付的订单
            Map paramMap = new HashMap();
            //参数
            paramMap.put(ConstantUtil.PayConfig.APP_ID, ConstantUtil.PayConfig.APPID);
            paramMap.put("mch_id", ConstantUtil.PayConfig.MCH_ID);//"1558950191"
            //    订单编号  不能重复     需要自己传递
            paramMap.put("out_trade_no", order.getOrderNum());
            //    支付总金额     需要自己传递
            paramMap.put("total_fee", new BigDecimal(order.getMoney()).multiply(new BigDecimal("100")).longValue() + ""); //单位 分
            //    支付描述   需要自己传递
            paramMap.put("body", order.getDescribe());
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            paramMap.put("spbill_create_ip", "127.0.0.1");
            paramMap.put("trade_type", "NATIVE");
            paramMap.put("notify_url", "http://www.chenkaixiang.top/notifyWeiXinPay\n");
            //统一下单
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantUtil.PayConfig.API_KEY));
            client.setHttps(true);
            client.post();
            //生成的预支付订单的消息
            String xml=client.getContent();//xml
            System.out.println("-------------"+xml);
            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
            System.out.println(resultMap);
            Map returnMap = new HashMap<>();
            //map.put("out_trade_no", orederNum);
            //map.put("course_id", 1);
            //map.put("total_fee", 1 + "");
            //map.put("result_code", resultMap.get("result_code"));
            returnMap.put("code_url", resultMap.get("code_url"));
            return returnMap;
        }catch(Exception e){
            // System.out.println(e.getMessage());
            log.error(e.getMessage());
        }
        return null;
    }

    @Override
    public Map getPayStatus(String orderId) {
        try {
            Map paramMap = new HashMap();
            //参数
            paramMap.put("appid", ConstantUtil.PayConfig.APPID);
            paramMap.put("mch_id", ConstantUtil.PayConfig.MCH_ID);//"1558950191"

            //订单编号    自己必须传递
            paramMap.put("out_trade_no",orderId);
            //1.00    1
            //m.put("total_fee", new BigDecimal(order.getPrice()*order.getNum()).multiply(new BigDecimal("100")).longValue() + ""); //单位 分
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            //m.put("sign", "127.0.0.1");
            //m.put("trade_type", "NATIVE");
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantUtil.PayConfig.API_KEY));
            client.setHttps(true);
            client.post();
            String xml = client.getContent();//xml
            System.out.println("支付后的信息========" + xml);
            //System.out.println("支付后的信息========" + xml);
            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
            System.out.println("支付之后的信息的内容==================="+resultMap);
            return resultMap;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public Map getNativeByOrder(Registration registration) {
        try {
            //统一下单   生成预支付的订单
            Map paramMap = new HashMap();
            //参数
            paramMap.put(ConstantUtil.PayConfig.APP_ID, ConstantUtil.PayConfig.APPID);
            paramMap.put("mch_id", ConstantUtil.PayConfig.MCH_ID);//"1558950191"
            //    订单编号  不能重复     需要自己传递
            paramMap.put("out_trade_no", registration.getCareerId());
            //    支付总金额     需要自己传递
            paramMap.put("total_fee", new BigDecimal(registration.getMoney()).multiply(new BigDecimal("100")).longValue() + ""); //单位 分
            //    支付描述   需要自己传递
            paramMap.put("body", "随便写");
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            paramMap.put("spbill_create_ip", "127.0.0.1");
            paramMap.put("trade_type", "NATIVE");
            paramMap.put("notify_url", "http://www.chenkaixiang.top/notifyWeiXinPay\n");
            //统一下单
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantUtil.PayConfig.API_KEY));
            client.setHttps(true);
            client.post();
            //生成的预支付订单的消息
            String xml=client.getContent();//xml
            System.out.println("-------------"+xml);
            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
            System.out.println(resultMap);
            Map returnMap = new HashMap<>();
            //map.put("out_trade_no", orederNum);
            //map.put("course_id", 1);
            //map.put("total_fee", 1 + "");
            //map.put("result_code", resultMap.get("result_code"));
            returnMap.put("code_url", resultMap.get("code_url"));
            return returnMap;
        }catch(Exception e){
            // System.out.println(e.getMessage());
            log.error(e.getMessage());
        }
        return null;
    }
}
